package lp.xian.production.service.impl;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DataUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MathUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.domain.Market;
import com.ruoyi.common.mapper.MarketMapper;
import lp.xian.production.vo.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lp.xian.production.mapper.ProductMapper;
import lp.xian.production.domain.Product;
import lp.xian.production.service.IProductService;

/**
 * 成品库存Service业务层处理
 *
 * @author ruoyi
 * @date 2023-07-06
 */
@Service
public class ProductServiceImpl implements IProductService
{
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private MarketMapper marketMapper;

    /**
     * 查询成品库存
     *
     * @param productId 成品库存主键
     * @return 成品库存
     */
    @Override
    public Product selectProductByProductId(Integer productId)
    {
        return productMapper.selectProductByProductId(productId);
    }

    /**
     * 查询成品库存列表
     *
     * @param product 成品库存
     * @return 成品库存
     */
    @Override
    public List<Product> selectProductList(Product product)
    {
        return productMapper.selectProductList(product);
    }

    /**
     * 新增成品库存
     *
     * @param product 成品库存
     * @return 结果
     */
    @Override
    public int insertProduct(Product product)
    {
        product.setCreateTime(DateUtils.getNowDate());
        return productMapper.insertProduct(product);
    }

    /**
     * 修改成品库存
     *
     * @param product 成品库存
     * @return 结果
     */
    @Override
    public int updateProduct(Product product)
    {
        product.setUpdateTime(DateUtils.getNowDate());
        return productMapper.updateProduct(product);
    }

    /**
     * 批量删除成品库存
     *
     * @param productIds 需要删除的成品库存主键
     * @return 结果
     */
    @Override
    public int deleteProductByProductIds(Integer[] productIds)
    {
        return productMapper.deleteProductByProductIds(productIds);
    }

    /**
     * 删除成品库存信息
     *
     * @param productId 成品库存主键
     * @return 结果
     */
    @Override
    public int deleteProductByProductId(Integer productId)
    {
        return productMapper.deleteProductByProductId(productId);
    }

    @Override
    public int goProduct(ProductVo productVo)throws ParseException {
        Market market=new Market();
        String orderNo=createNo(productVo.getProductId().toString());
        market.setOrderNo(orderNo);
        market.setMarketName(productVo.getProductName());
        market.setMarketNum(productVo.getProductNum());
        market.setMarketPrice(productVo.getProductPrice());
        market.setMarketUnit(productVo.getProductUnit());
        market.setClientId(productVo.getClientId());
        market.setOrderStatus(1);
        market.setAuditStatus(0);
        market.setCreateBy(SecurityUtils.getUsername());
        market.setCreateTime(new Date());
        //需要减库存操作
        int num=marketMapper.insertMarket(market);
        if (num>0){
            Product product=productMapper.selectProductByProductId(productVo.getProductId());
            Integer nums=product.getProductNum()-productVo.getProductNum();
            product.setProductNum(nums);
            product.setProductId(productVo.getProductId());
            return productMapper.updateProduct(product);
        }
        return 1;
    }
    public String createNo(String id)throws ParseException{
        String no="";
        StringBuffer buffer=new StringBuffer(DataUtils.format(new Date(),DateUtils.YYYYMMDDHHMMSS));
        buffer.append(MathUtils.random());
        if (id.length()<6){
            for(int i=6-id.length();i>0;i--){
                buffer.append("0");
            }
        }
        no=buffer.append(id).toString();
        Map<String,Object> params=new HashMap<String, Object>();
//        if (type==1){
//            params.put("appointCode",no);
//            List<Appointment> appointmentList=appointmentMapper.getAppointmentListByMap(params);
//            if (appointmentList!=null){
//                createNo(1,id);
//            }
//        }else if(type==2){
//            List<MaintainOrder> orderList=maintainOrderMapper.getMaintainOrderListByMap(params);
//            if (orderList!=null){
//                createNo(2,id);//递归算法
//            }
//        }
        return no;
    }
}
